package com.waymap.task;

import com.waymap.Thread.WmsThread;
import com.waymap.Thread.WmsThreads;
import com.waymap.domain.Transactions;
import com.waymap.mapper.TransactionsMapper;
import com.waymap.utils.mq.MsgProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@Component
@Configuration
@EnableScheduling
@Slf4j
public class SyncTxScheduledTasks {
    @Resource
    private TransactionsMapper transactionsMapper;
    @Autowired
    private MsgProducer producer;
    /**
     * 查询所有没有同步的tx  循环同步
     * 每两小时执行一次同步
     * 每15分钟执行一次同步
     */
    //@Scheduled(cron = "0 0 0/2 * * ? ")
    @Async
    @Scheduled(cron = "0 0/15 * * * ?")
    public void ScheduledTasks() {
        log.info("查询所有未同步transactions");
        System.out.println("ScheduledTasks name " + Thread.currentThread().getName());
        List<Transactions> noSendToNSData = transactionsMapper.getNoSendToNS();
        if(noSendToNSData.size()>0){
            for (Transactions txObject : noSendToNSData) {
                log.info("开始同步Tranid:{} ,TransactionType: {} ",txObject.getTranid(),txObject.getTransactionType());
                /*****利用线程发送ACTIVE_MQ模块*****/
                //WmsThreads.execute(producer,txObject.getTransactionType(), txObject.getTranid(),txObject.getTransactionType(), txObject.getAccountId());
                //WmsThreads.execute(producer,txObject.getTransactionType(), txObject.getTranid(),txObject.getTransactionType(), txObject.getAccountId());
                WmsThread wms = new WmsThread("wms-mq", txObject.getTranid(), txObject.getTransactionType(), producer, txObject.getTransactionType(), txObject.getAccountId());
                wms.start();
            }
        }else{
            log.info("所有transactions已同步");
        }
    }


}

